/*
 * Copyright (c) 2025. hallele, Inc All rights reserved.
 */

package cn.hallele.ms.starter.web.cc;

import cn.hallele.infrastructure.util.ObjectUtil;
import cn.hallele.infrastructure.util.SpringUtil;
import cn.hallele.ms.starter.web.cc.data.DataDict;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * 系统配置数据
 *
 * @author anle5
 * @since 2025/3/5 23:29
 */
public class CCData {
    private static final RedisTemplate<String, Object> REDIS = SpringUtil.getBean("stringRedisTemplate");

    /**
     * 数据字典
     */
    private static final String CACHE_DATA_DICT = "CACHE:DATA_DICT";

    /**
     * 获取字典code下的所有字典
     *
     * @param code 字典code
     * @return 所有字典
     */
    public DataDict getDataDict(String code) {
        return ObjectUtil.cast(REDIS.boundHashOps(CACHE_DATA_DICT).get(code));
    }

    /**
     * 获取请求语言的字典值
     *
     * @param code 字典code
     * @param key  字典key
     * @return 字典值
     */
    public String getDataDictValue(String code, String key) {
        DataDict dataDict = getDataDict(code);

        if (dataDict != null) {
            return dataDict.getDataDictValue(key);
        }

        return null;
    }
}
